refs: allow to specify multiple refs as args
authorGiuseppe Scrivano <gscrivan@redhat.com>
Mon, 29 Feb 2016 10:14:59 +0000 (11:14 +0100)
committerColin Walters <walters@verbum.org>
Wed, 2 Mar 2016 19:52:02 +0000 (14:52 -0500)
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
man/ostree-refs.xml
src/ostree/ot-builtin-refs.c

index e97cb1c5af9c8eb644178edf1626fcf006a1f358..6d75d3651cbc0cd9492c84cc4eb4e266688398f9 100644 (file)
@@ -57,7 +57,7 @@ Boston, MA 02111-1307, USA.
     <refsect1>
         <title>Description</title>
         <para>
-            Lists all refs available on the host.  If pecified, PREFIX assigns the refspec prefix; default prefix is null, which lists all refs.
+            Lists all refs available on the host.  If specified, PREFIX assigns the refspec prefix; default prefix is null, which lists all refs.
         </para>
     </refsect1>
 
index 575f6646bdab259c07c346959730db7e99d4782b..dabb285cd22f6988fe20e4ac926e74a73b12b45a 100644 (file)
@@ -33,39 +33,18 @@ static GOptionEntry options[] = {
   { NULL }
 };
 
-gboolean
-ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **error)
+static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellable *cancellable, GError **error)
 {
-  gboolean ret = FALSE;
-  GOptionContext *context;
-  glnx_unref_object OstreeRepo *repo = NULL;
-  const char *refspec_prefix = NULL;
   g_autoptr(GHashTable) refs = NULL;
   GHashTableIter hashiter;
   gpointer hashkey, hashvalue;
-
-  context = g_option_context_new ("[PREFIX] - List refs");
-
-  if (!ostree_option_context_parse (context, options, &argc, &argv, OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
-    goto out;
-
-  if (argc >= 2)
-    refspec_prefix = argv[1];
-
-  /* Require a prefix when deleting to help avoid accidents. */
-  if (opt_delete && refspec_prefix == NULL)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "PREFIX is required when deleting refs");
-      goto out;
-    }
-
-  if (!ostree_repo_list_refs (repo, refspec_prefix, &refs,
-                              cancellable, error))
-    goto out;
+  gboolean ret = FALSE;
 
   if (!opt_delete)
     {
+      if (!ostree_repo_list_refs (repo, refspec_prefix, &refs, cancellable, error))
+        goto out;
+
       g_hash_table_iter_init (&hashiter, refs);
       while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
         {
@@ -75,6 +54,10 @@ ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **
     }
   else
     {
+      if (!ostree_repo_list_refs_ext (repo, refspec_prefix, &refs, OSTREE_REPO_LIST_REFS_EXT_NONE,
+                                      cancellable, error))
+        goto out;
+
       g_hash_table_iter_init (&hashiter, refs);
       while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
         {
@@ -90,6 +73,41 @@ ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **
             goto out;
         }
     }
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+gboolean
+ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **error)
+{
+  gboolean ret = FALSE;
+  GOptionContext *context;
+  glnx_unref_object OstreeRepo *repo = NULL;
+  int i;
+
+  context = g_option_context_new ("[PREFIX] - List refs");
+
+  if (!ostree_option_context_parse (context, options, &argc, &argv, OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
+    goto out;
+
+  if (argc >= 2)
+    {
+      for (i = 1; i < argc; i++)
+        if (!do_ref (repo, argv[i], cancellable, error))
+          goto out;
+    }
+  else
+    {
+      /* Require a prefix when deleting to help avoid accidents. */
+      if (opt_delete)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "At least one PREFIX is required when deleting refs");
+          goto out;
+        }
+      ret = do_ref (repo, NULL, cancellable, error);
+    }
 
   ret = TRUE;
  out: